﻿using System;
using System.Configuration;
using mojoPortal.Net;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using ud_gSDMS.Code_Service;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web.Editor;
using System.Linq;
using System.Data;
using Telerik.Web.UI;

namespace ud_gSDMS.Testing
{
    public partial class CloseTestProject : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private SiteUser _siteUser;
        private int id = -1;


        void Page_Load(object sender, EventArgs e)
        {
            //Page.Title = "Send Require";
            //if (!UserCanEditPage() || !UserCanViewPage())
            //{
            //    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            //}

            

            //SiteUser.ConfirmRegistration()
            if (_siteUser == null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                if (gSDMS_BL.mp_User_IsRole(_siteUser.UserId, "3") == false)
                {
                    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
                }
            }


            //init database
        }

        #region event
        void btnSave_Click(object sender, EventArgs e)
        {
            //Send email
            if (this.CheckBSendMail.Checked)
            {
                var userManager = gSDMS_BL.mp_User_get(null, radComBBSender.Text);
                var assigned = gSDMS_BL.mp_User_get(null, radComBBSendTo.Text );
                var status = ComboBoxStatus.SelectedItem.ToString() == "Closed" ? "CLOSED" : "REOPENED";
                var cc = gSDMS_BL.ud_gSDMS_PROJECT_TEST_Get(id);

                var emailFrom = userManager.Email;
                var emailCc = cc.ud_gSDMS_PROJECT.mp_User.Email;
                var emailTo = assigned.Email;

                var subject = string.Format("[gSDMS-TESTING] {0} PROJECT : {1} FROM gSDMS", status, radComBBProjectCode.Text);
                var textBody =
                                "Dear " + assigned.LoginName + "<br/><br/> Test project has been " + status.ToLower() + " : <b>" + radComBBProjectCode.Text + 
                                "</b>. <br/><br/> Please enter follow link to check: " + SiteRoot + "/gsdmsmodule/Testing/Error_Lists.aspx?pageid=9&id=" + id +
                                " <br/><br/> <b><u>Feedback Note</b></u> : " + txtNotes.Text + "<br/><br/> Regard, <br/>" + userManager.LoginName;
                var siteGuid = siteSettings.SiteGuid;
                Library.SendMail(emailFrom, emailTo, emailCc, string.Empty, subject, textBody, siteGuid);
                
              
            }

            //Update
            gSDMS_BL.ud_gSDMS_PROJECT_TESTs_Upd(int.Parse(this.radComBBProjectCode.SelectedValue), -1, null, -1, -1, null, null, this.txtNotes.Text, int.Parse(ComboBoxStatus.SelectedValue) , true);


            WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/testing/Error_Lists.aspx?pageid=9&testid=" + id);

        }

        #endregion event

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            _siteUser = SiteUtils.GetCurrentSiteUser();
            Load += Page_Load;

        }

        private void LoadData()
        {
            //Load Project code and add to comBBProjectCode
            var temp = gSDMS_BL.ud_gSDMS_PROJECT_TEST_Get(id);

            this.radComBBProjectCode.DataValueField = "Project_ID";
            this.radComBBProjectCode.DataTextField = "Code";
            this.radComBBProjectCode.Items.Add(new RadComboBoxItem(temp.ud_gSDMS_PROJECT.Code ,temp.Project_Test_ID.ToString()));
            

            //Load Close date
            this.datCloseDate.SelectedDate = DateTime.Today;

            //Load Sender la nguoi test project

            var users = gSDMS_BL.mp_User_Lst("3");
            radComBBSender. DataValueField = "Email";
            radComBBSender.DataTextField = "LoginName";
            radComBBSender.DataSource = users;
            radComBBSender.DataBind();
            radComBBSender.SelectedValue = _siteUser.UserId.ToString();

            var projectTest = gSDMS_BL.ud_gSDMS_PROJECT_TEST_Get(id);
            var assign = gSDMS_BL.getMemberList(projectTest.ud_gSDMS_PROJECT.Project_ID.ToString());
            radComBBSendTo. DataValueField = "Email";
            radComBBSendTo.DataTextField = "LoginName";
            radComBBSendTo.DataSource = assign;
            radComBBSendTo.DataBind();
            radComBBSendTo.SelectedValue = projectTest.mp_User.Email; 

            //Add status item
            var status = gSDMS_BL.ud_gSDMS_CAT_PROJECT_TEST_STATUS_Lst();
            this.ComboBoxStatus.DataValueField = "CAT_Project_Test_Status_ID";
            this.ComboBoxStatus.DataTextField = "Name";
            this.ComboBoxStatus.DataSource = status;
            this.ComboBoxStatus.DataBind();
            this.ComboBoxStatus.SelectedValue = "2";
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);

            SuppressPageMenu();

                btnSave.Click += btnSave_Click;
                LoadData();
        }

        private void LoadParams()
        {
            id = WebUtils.ParseInt32FromQueryString("testid", true, id);
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}